package com.sl.spring.intergration.rabbitmq.header;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.AMQP.BasicProperties.Builder;
import com.rabbitmq.client.Channel;

@Component
public class HeaderSend {

	@Autowired
	private ConnectionFactory connectionFactory;

	private final static String exchange = "header-exchange";

	public void send() throws Exception {
		Connection c = null;
		Channel channel = null;
		try {
			c = connectionFactory.createConnection();
			channel = c.createChannel(false);
			 channel.exchangeDeclare(exchange, ExchangeTypes.HEADERS); 
			
			Map<String, Object> header = new HashMap<String, Object>();
			header.put("testdsds", "testdd");
			Builder properties = new BasicProperties.Builder();
			properties.headers(header);

			String message = "testtest";

			channel.basicPublish(exchange, "", properties.build(),
					message.getBytes());

		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(c!= null){
				c.close();
			}
			if(channel != null){
				channel.close();
			}
		}
	}

}
